import { message } from 'antd';
import {
  getAccuSearchList,
  updateAccuStatus,
  getAccuList,
  deleteAccuMul,
} from '@/services/bigaccomanage';

export default {
  state: {
    activeKey: '1',
    tabList: [
      { itemCode: '1', itemName: '社保' },
      { itemCode: '2', itemName: '公积金' },
      { itemCode: '3', itemName: '工伤二级户' },
    ],
    formFieldsValue: {},
    expandForm: false,
    bigwareList: [],
    selectedRows: [],
    selectedRowKeys: [],
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
  },

  effects: {
    // 获取大库查询列表
    *fetchBigwareList({ payload }, { call, put }) {
      const response = yield call(getAccuSearchList, payload);
      if (response.success) {
        yield put({
          type: 'getBigwareList',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
    },
    // // 获取工伤二级户查询列表
    // *fetchInjuryList({ payload }, { call, put }) {
    //   const response = yield call(getInjuryList, payload);
    //   if (response.success) {
    //     yield put({
    //       type: 'getInjuryList',
    //       payload: response.data,
    //     });
    //   } else {
    //     message.warning(response.message);
    //   }
    // },
    // *fetchSocialList({ payload }, { call, put }) {
    //   const response = yield call(getSocialList, payload);
    //   const {
    //     pageArg: { currentPage, pageSize },
    //   } = payload;
    //   if (response.success) {
    //     yield put({
    //       type: 'getSocialList',
    //       payload: { ...response, currentPage, pageSize },
    //     });
    //   } else {
    //     message.warning(response.message);
    //   }
    // },
    *fetchAccuList({ payload }, { call, put }) {
      const response = yield call(getAccuList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getAccuList',
          payload: { ...response, currentPage, pageSize },
        });
      } else {
        message.warning(response.message);
      }
    },
    // *fetchInjuryPageList({ payload }, { call, put }) {
    //   const response = yield call(getInjuryPageList, payload);
    //   const {
    //     pageArg: { currentPage, pageSize },
    //   } = payload;
    //   if (response.success) {
    //     yield put({
    //       type: 'getInjuryPageList',
    //       payload: { ...response, currentPage, pageSize },
    //     });
    //   } else {
    //     message.warning(response.message);
    //   }
    // },
    *deleteAccuMul({ payload }, { call, put, select }) {
      const response = yield call(deleteAccuMul, payload);
      if (response.success) {
        const formFieldsValue = yield select(state => state.accuware.formFieldsValue);
        const current = yield select(state => state.accuware.pageData.pagination.current);
        const pageSize = yield select(state => state.accuware.pageData.pagination.pageSize);
        yield put({
          type: 'fetchAccuList',
          payload: {
            ...formFieldsValue,
            pageArg: {
              currentPage: current,
              pageSize,
            },
          },
        });
        message.success(`${response.message || '删除成功！'}`);
      } else {
        message.warning(`${response.message || response.data || '删除失败！'}`);
      }
    },
    *updateStatus({ payload }, { call, put, select }) {
      const warnMessage = Number(payload.accoStatus) === 2 ? '停用成功' : '启用成功';
      const response = yield call(updateAccuStatus, payload);
      if (response.success) {
        const formFieldsValue = yield select(state => state.accuware.formFieldsValue);
        const current = yield select(state => state.accuware.pageData.pagination.current);
        const pageSize = yield select(state => state.accuware.pageData.pagination.pageSize);
        yield put({
          type: 'fetchAccuList',
          payload: {
            unitName: formFieldsValue.accuUnitName && formFieldsValue.accuUnitName.trim(),
            accuAccoId: formFieldsValue.accuAccoId && formFieldsValue.accuAccoId.trim(),
            areaId: formFieldsValue.accuAreaId && formFieldsValue.accuAreaId.trim(),
            accoStatus: formFieldsValue.accoStatus && formFieldsValue.accoStatus.trim(),
            pageArg: {
              currentPage: current,
              pageSize,
            },
          },
        });
        message.success(`${warnMessage}`);
      } else {
        message.warning(`${response.message || '修改失败！'}`);
      }
    },
  },

  reducers: {
    // 获取列表
    getSocialList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        selectedRowKeys: [],
        selectedRows: [],
        pageData: {
          list,
          pagination: {
            total: data.totalCount || 0,
            current: currentPage,
            pageSize,
          },
        },
      };
    },

    // 获取列表
    getAccuList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        selectedRowKeys: [],
        selectedRows: [],
        pageData: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },

    // // 获取工伤二级户列表
    // getInjuryPageList(
    //   state,
    //   {
    //     payload: { data, currentPage, pageSize },
    //   }
    // ) {
    //   const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
    //   return {
    //     ...state,
    //     selectedRowKeys: [],
    //     selectedRows: [],
    //     pageData: {
    //       list,
    //       pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
    //     },
    //   };
    // },

    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 获取大库下拉列表
    getBigwareList(state, { payload }) {
      return {
        ...state,
        bigwareList: payload,
      };
    },
    // // 获取工伤二级户下拉列表
    // getInjuryList(state, { payload }) {
    //   return {
    //     ...state,
    //     injuryList: payload,
    //   };
    // },

    //
  },
};
